package org.bozo.im.service.protocols.xmpp.stanza.processor.jabber.client;

import java.io.IOException;

import org.bozo.im.service.protocols.xmpp.XMPPSession;
import org.bozo.im.service.protocols.xmpp.stanza.Stanza;
import org.bozo.im.service.protocols.xmpp.stanza.processor.StanzaProcessor;
import org.bozo.im.service.protocols.xmpp.stanza.util.jabber.client.Type;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

public class IqStanzaProcessor extends StanzaProcessor {

	private static IqStanzaProcessor mInstance;

	private IqStanzaProcessor() {}

	public static IqStanzaProcessor getInstance() {
		if (mInstance == null)
			mInstance = new IqStanzaProcessor();
		return mInstance;
	}

	@Override
	public Stanza getStanza() {
		return Stanza.JABBER_CLIENT_IQ;
	}

	@Override
	public void processStanza(XmlPullParser pParser, XMPPSession pSession, StanzaProcessor pParentProcessor) throws XmlPullParserException,	IOException {

		String lId = null;
		Type lType = null;

		for (int i = 0; i < pParser.getAttributeCount(); i++) {
			if (pParser.getAttributeName(i).equals("id")) {
				lId = pParser.getAttributeValue(i);

			} else if (pParser.getAttributeName(i).equals("type")) {
				lType = Type.valueOfType(pParser.getAttributeValue(i));
			}
		}

		if (lType == Type.RESULT && lId != null)
			pSession.getTaskProcessor().processTasks(lId);

		super.processStanza(pParser, pSession, pParentProcessor);
	}
}
